package com.ailk.aee.pcc.dao;

import com.ailk.aee.pcc.mongo.Mongo;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenchao
 */
@Component
public class UserPloyDao {

    @Autowired
    private Mongo userPloyMongo;

    public DBObject queryOne(String userId) {
        DBObject query = new BasicDBObject().append("userId", userId);
        return userPloyMongo.getContentCollection().findOne(query);
    }

    public void deleteToMongo(String userId, String ployId) {
        DBObject query = new BasicDBObject().append("userId", userId);
        DBObject unset = new BasicDBObject().append("$unset", new BasicDBObject().append(ployId, 1));
        userPloyMongo.getContentCollection().update(query, unset, true, false);
    }

    public void insertToMongo(String userId, String ployId) {
        DBObject query = new BasicDBObject().append("userId", userId);

        List<String> list = new ArrayList<String>();
        DBObject upsert = new BasicDBObject("$addToSet", new BasicDBObject().append(ployId, new BasicDBObject("$each", list)));
        userPloyMongo.getContentCollection().update(query, upsert, true, false);
    }
}
